<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Apk;

class ApkController extends Controller
{
    protected $message = [
        
        'app_name.required' => '名称必须',
        'app_version.required' => '版本号必须',
        'app_version.regex' => '版本号格式不正确',
        'app_version.unique' => '版本号已存在',
        
        'app_type.required' => '分类必须',
        'app_type.integer' => '分类由数字组成',
        'app_type.in' => '分类非法',
        
        'app_phone.required' => '手机类型必须',
        'app_phone.integer' => '手机类型由数字组成',
        'app_phone.in' => '手机类型非法',
        
        'app_update_type.required' => '更新方式必须',
        'app_update_type.integer' => '更新方式由数字组成',
        'app_update_type.in' => '更新方式非法',
        
        'app_path.required' => '包文件必须',

    ];
    
    /**
     * @api {get} /apk/ 列表
     * @apiDescription app列表
     * @apiGroup app管理
     * @apiPermission none
     * @apiSuccess {String} code 状态标志 
     * @apiSuccess {String} message 提示信息 
     * @apiSuccess {Object[]} data  区域数据
     * @apiSuccessExample {json} 成功:
     *     HTTP/1.1 201
     *     {
     *         "code": 0,
     *         "message": "",
     *          "data": [
     *             {
     *             }
     *          ]
     *     }
     * @apiErrorExample {json} 失败:
     *     HTTP/1.1 422
     *     {
     *       "code": -1,
     *       "message": "错误提示"
     *     }
     */
    public function index(Request $request)
    {
        $perPage = $request->input('pageSize',15);
        $name = $request->input('app_name', '');
        $ver = $request->input('app_version', '');
        $query = Apk::query();
        if(!empty($name)) {
            $query->where('app_name', 'like', $name.'%');
        }
        if(!empty($name)) {
            $query->where('app_version', 'like', $ver.'%');
        }
        $users = $query-> paginate($perPage);
        
        $configData = $this->getConfigData();
        
        return array_merge([ 'data' => $users ], $configData);
    }
    
    //获取配置数据
    private function getConfigData() {
        $keys = ['app_type', 'app_phone', 'app_update_type'];
        $result = [];
        foreach ($keys as $key) {
            $config = config('site.'.$key);
            $result[$key] = $config;
            $result[$key.'_label'] = collect($config)->pluck('label','value')->all();         
        }
        return $result;
    }
    
    //获取配置数据规则
    private function getConfigRules() {
        $keys = ['app_type', 'app_phone', 'app_update_type'];
        $result = [];
        foreach ($keys as $key) {
            $config = config('site.'.$key);
            $types = collect($config)->pluck('value')->all();
            $result[$key] = 'required|integer|in:'. implode(',', $types);         
        }
        return $result;
    }


    /**
     * @api {post} /area/ 添加
     * @apiDescription 添加app
     * @apiGroup app管理
     * @apiPermission none
     * @apiSuccessExample {json} 成功:
     *     HTTP/1.1 201
     *     {
     *         "code": 0,
     *         "message": "",
     *              
     *     }
     * @apiErrorExample {json} 失败:
     *     HTTP/1.1 422
     *     {
     *       "code": -1,
     *       "message": "添加失败"
     *     }
     */
    public function store(Request $request)
    {
       $data = $request->input();
       $this->validateData($request);
       $data['creater_id'] = $request->user()->userid;
       $data['creater_name'] = $request->user()->username;
       $data['Co_ID'] = $request->user()->Co_ID;
       $ret = Apk::create($data);
       
       if(!$ret){
           $this->error('添加失败');
       }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }


    /**
     * @api {put} /apk/:id 更新
     * @apiDescription 更新app
     * @apiGroup app管理
     * @apiPermission none
     * @apiParam {Number} id app编号
     * @apiSuccessExample {json} 成功:
     *     HTTP/1.1 201
     *     {
     *         "code": 0,
     *         "message": "",
     *              
     *     }
     * @apiErrorExample {json} 失败:
     *     HTTP/1.1 422
     *     {
     *       "code": -1,
     *       "message": "保存失败"
     *     }
     */
    public function update(Request $request, $id)
    {
       $deviceType = Apk::findOrFail($id);
       $data = $request->input();
       $this->validateData($request, $id);
       $data['creater_id'] = $request->user()->userid;
       $data['creater_name'] = $request->user()->username;
       $data['Co_ID'] = $request->user()->Co_ID;
       $ret = $deviceType->update($data);
       
       if($ret===false){
           $this->error('保存失败');
       }
    }
    
    //表单验证
    protected function validateData($request, $id = false) {
        $versionRule = ['required', 'regex:/^\d+\.\d+\.\d+$/', 'unique:apks'];
        if($id) {
            $versionRule =['required', 'unique:apks,app_version,'.$id];
        }

        $configRules = $this->getConfigRules();
        
        $this->validate($request, array_merge([
            'app_name' => 'required',
            'app_version' => $versionRule,
          //  'app_path' => 'required',
        ], $configRules), $this->message);
    }

    /**
     * @api {delete} /apk/:id 删除
     * @apiDescription 删除app
     * @apiGroup app管理
     * @apiPermission none
     * @apiParam {Number} id app编号
     * @apiSuccessExample {json} 成功:
     *     HTTP/1.1 201
     *     {
     *         
     *          "code": 0,
     *          "message": "",
     *        
     *     }
     * @apiErrorExample {json} 失败:
     *     HTTP/1.1 422
     *     {
     *       "code": -1,
     *       "message": "错误提示"
     *     }
     */
    public function destroy($id)
    {
       $deviceType = Apk::findOrFail($id);
       if(!$deviceType->delete()){
           $this->error('删除失败');
       }
    }
}
